package com.feidian.helper.ui;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.feidian.helper.R;
import com.feidian.helper.bean.Table;
import com.feidian.helper.bean.User;
import com.feidian.helper.net.NetManager;
import com.feidian.helper.ui.RegisterActivity.RegisterThread;

import eidian.sail.entity.Listener.ImageLoadListener;
import eidian.sail.entity.Listener.LoginListener;
import eidian.sail.entity.Student;
import eidian.sail.helper.HttpHelper;

/**
 * 
 * @author lxq
 * @category category 重置密码界面
 * 
 */
public class ResetPasswordActivity extends Activity implements OnClickListener,
		ImageLoadListener, LoginListener {

	private static final int IMAGE_LOAD_SUCCESS = 0X111;
	private static final int IMAGE_LOAD_FAILURE = 0X112;
	private static final int SET_PASSWORD_SUCCESS = 0X113;
	private static final int SET_PASSWORD_FAILURE = 0X114;
	private static final int LOGIN_SUCCESS = 0X115;
	private static final int LOGIN_FAILURE = 0X116;
	private static final int GET_CONTENT = 0X117;

	private EditText stuIdET;// 学号
	private EditText OpasswordET;// 教务处
	private EditText passwordET; // 修改密码
	private EditText surePasswordET;// 确认密码
	private EditText checkCodeET; // 验证码
	private ImageView checkCodeIV;
	private Button sureBn;

	private Bitmap mBitmap;
	private HttpHelper mHttpHelper;
	private Student mStudent;
	Handler mHandler = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case IMAGE_LOAD_SUCCESS:

				checkCodeIV.setImageBitmap(mBitmap);
				break;
			case LOGIN_SUCCESS:
				User.stuId = mStudent.getID();
				User.name = mStudent.getName();
				User.password = mStudent.getPswd();
				Log.e("个人信息:",
						"姓名" + mStudent.getName() + ",学号：" + mStudent.getID()
								+ ",密码：" + mStudent.getPswd());
				String url = "http://115.29.103.105/User/SetPassword";
				List<NameValuePair> params = new ArrayList<NameValuePair>();
				params.add(new BasicNameValuePair(Table.stuId, mStudent.getID()));
				Log.e("password=",passwordET
						.getText().toString().trim());
				params.add(new BasicNameValuePair(Table.password, passwordET
						.getText().toString().trim()));
				SetInfPassword thread = new SetInfPassword(url, params);
				thread.start();
				break;
			case IMAGE_LOAD_FAILURE:
				Toast.makeText(ResetPasswordActivity.this, "获取验证码失败",
						Toast.LENGTH_SHORT).show();
				break;
			case LOGIN_FAILURE:
				Toast.makeText(ResetPasswordActivity.this, "链接失败",
						Toast.LENGTH_SHORT).show();
				break;
			case GET_CONTENT:
				Log.e("getContext=", (String) msg.obj);
				Toast.makeText(ResetPasswordActivity.this,
						"成功修改密码" + (String) msg.obj, Toast.LENGTH_LONG).show();
				User.password = passwordET.getText().toString().trim();
				startActivity(new Intent(ResetPasswordActivity.this,
						MainFragmentActivity.class));
				break;
			}
		};
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.activity_forget);
		initView();
		getImage();
	}

	void initView() {

		stuIdET = (EditText) findViewById(R.id.stuId_edit);
		if (User.stuId != null)
			stuIdET.setText(User.stuId);
		OpasswordET = (EditText) findViewById(R.id.origin_userpassword_edit);
		passwordET = (EditText) findViewById(R.id.password_edit);
		surePasswordET = (EditText) findViewById(R.id.sure_password);
		checkCodeET = (EditText) findViewById(R.id.user_code);
		checkCodeIV = (ImageView) findViewById(R.id.checkcode_image);
		checkCodeIV.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				getImage();
			}
		});
		sureBn = (Button) findViewById(R.id.sure_bt);
		sureBn.setOnClickListener(this);
		mHttpHelper = new HttpHelper(this);

	}

	private void getImage() {
		mHttpHelper.getCheckImage(this);
	}

	@Override
	public void imageLoadFailBack(String arg0) {
		// TODO Auto-generated method stub
		Log.e("imageLoadFailBack","imageLoadFailBack");
		Message msg = mHandler.obtainMessage();
		msg.what = IMAGE_LOAD_FAILURE;
		mHandler.sendMessage(msg);

	}

	@Override
	public void imageLoadSuccessBack(Bitmap arg0) {
		// TODO Auto-generated method stub
		Log.e("imageLoadSuccessBack","imageLoadSuccessBack");
		this.mBitmap = arg0;
		Message msg = mHandler.obtainMessage();
		msg.what = IMAGE_LOAD_SUCCESS;
		mHandler.sendMessage(msg);
	}

	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub
		switch (arg0.getId()) {
		case R.id.sure_bt: {
			passwordET.getText();
			surePasswordET.getText();
			if (!passwordET.getText().toString().trim().equals(
					surePasswordET.getText().toString().trim())) {
				Toast.makeText(ResetPasswordActivity.this, "修改密码和确认密码不匹对",
						Toast.LENGTH_LONG).show();
				return;
			}
			try {
				mHttpHelper.login(this, new Student(stuIdET.getText()
						.toString().trim(), OpasswordET.getText().toString()
						.trim()), checkCodeET.getText().toString().trim());
			} catch (NumberFormatException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

			break;
		case R.id.checkcode_image: {
			getImage();
		}
			break;
		default:
			break;
		}

	}

	@Override
	public void onFailBack(String arg0) {
		// TODO Auto-generated method stub
		mHttpHelper.getCheckImage(ResetPasswordActivity.this);
		Message msg = mHandler.obtainMessage();
		msg.what = LOGIN_FAILURE;
		mHandler.sendMessage(msg);
	}

	@Override
	public void onSuccessBack(Student student) {
		// TODO Auto-generated method stub
		mStudent = student;
		Message msg = mHandler.obtainMessage();
		msg.what = LOGIN_SUCCESS;
		mHandler.sendMessage(msg);

	}

	class SetInfPassword extends Thread {
		private String mUrl;
		private List<NameValuePair> mParams;

		public SetInfPassword(String url, List<NameValuePair> params) {
			// TODO Auto-generated constructor stub
			mUrl = url;
			mParams = params;
		}

		@Override
		public void run() {
			// TODO Auto-generated method stub
			String context = NetManager.getNetManager().getWebWithPost(mUrl,
					mParams);
			if (context != null)
				Log.e("contenxt=", context);
			else
				Log.e("context=", "null");
			Message msg = mHandler.obtainMessage();
			msg.obj = context;
			msg.what = GET_CONTENT;
			mHandler.sendMessage(msg);
			super.run();
		}
	}

}
